package znbkTopicType.resContent;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\19 0019.
 */

public class ZnbkContentResStlPptAdapter extends WrapBaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private ViewHolder holder = null;
    private class ViewHolder{
        WebView WvTheme;//大题文本题干区

        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
        }
    }

    /*
   * 获取小题选项信息
   * */
    public ZnbkContentResStlPptAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_stl_pt_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
        * 获取作文题用户相关信息
        * */
        getQuesInfor();

        /*
        * 显示题干信息
        * */
        showQuesInfor();


        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
//    private String GenreName = "";
//    private String TypeName = "";
//    private String CreateTime = "";
//    private int RecommendNumberOfTimes = -1;
    private String subChildAsk = "";
    private String subChildAnswer = "";
    public static int TotalItemCount = -1;
    public static int sumOption;//选项的个数
    public static String[] optionIndex;//选项号
    public static String[] optionContent;//选项内容对应的文字
    public static String[] smallSubChileAnswer;
    public static String analysis;

    private void  getQuesInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {

                /*
                * 正确答案：subChildAnswer
                * 小题数目：TotalItemCount
                * 小题答案：smallSubChileAnswer[i]
                * */
                    subChildAnswer = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                    TotalItemCount = Control.backSum(subChildAnswer,"$、")+1;
                    smallSubChileAnswer = new String[TotalItemCount];
                    for(int i=0;i<TotalItemCount;i++){
                        smallSubChileAnswer[i] = Control.returnArray(subChildAnswer,"$、",2)[i];
                        Log.e("smallSubChileAnswer==","==i=="+i+"==Sa=="+smallSubChileAnswer[i]);
                    }

                /*
                * 选项个数：sumOption
                * 选项题号：optionIndex[i]
                * 选项内容：optionContent[i]
                * */
                    sumOption = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().size();
                    optionIndex = new String[sumOption];
                    optionContent = new String[sumOption];
                    for (int i = 0; i < sumOption; i++) {
                        optionIndex[i] = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionIndex();
                        optionContent[i] = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionContent();
                    }

                /*
                * 文本内容：词汇填空
                * */
                    subChildAsk = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                    String arraySubChildAsk[] = new String[TotalItemCount + 1];
                    String subChildAskAll = "";
                    for (int i = 0; i < TotalItemCount + 1; i++) {
                        arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                        if (i < TotalItemCount) {
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                        } else {
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                        }
                    }
                    subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

                /*
                * 答案解析：analysis
                **/
                    analysis = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){


                    /*
                    * 正确答案：subChildAnswer
                    * 小题数目：TotalItemCount
                    * 小题答案：smallSubChileAnswer[i]
                    * */
                subChildAnswer = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                TotalItemCount = Control.backSum(subChildAnswer,"$、")+1;
                smallSubChileAnswer = new String[TotalItemCount];
                for(int i=0;i<TotalItemCount;i++){
                    smallSubChileAnswer[i] = Control.returnArray(subChildAnswer,"$、",2)[i];
                    Log.e("smallSubChileAnswer==","==i=="+i+"==Sa=="+smallSubChileAnswer[i]);
                }

                    /*
                    * 选项个数：sumOption
                    * 选项题号：optionIndex[i]
                    * 选项内容：optionContent[i]
                    * */
                sumOption = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().size();
                optionIndex = new String[sumOption];
                optionContent = new String[sumOption];
                for (int i = 0; i < sumOption; i++) {
                    optionIndex[i] = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionIndex();
                    optionContent[i] = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionContent();
                }


                    /*
                    * 文本内容：词汇填空
                    * */
                subChildAsk = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                String arraySubChildAsk[] = new String[TotalItemCount + 1];
                String subChildAskAll = "";
                for (int i = 0; i < TotalItemCount + 1; i++) {
                    arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                    if (i < TotalItemCount) {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                    } else {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                    }
                }
                subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

                    /*
                    * 答案解析：analysis
                    **/
                analysis = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }else if(ModelFrom==ZnbkPaperDef.FROM_NOTE){


                    /*
                    * 正确答案：subChildAnswer
                    * 小题数目：TotalItemCount
                    * 小题答案：smallSubChileAnswer[i]
                    * */
                subChildAnswer = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                TotalItemCount = Control.backSum(subChildAnswer,"$、")+1;
                smallSubChileAnswer = new String[TotalItemCount];
                for(int i=0;i<TotalItemCount;i++){
                    smallSubChileAnswer[i] = Control.returnArray(subChildAnswer,"$、",2)[i];
                    Log.e("smallSubChileAnswer==","==i=="+i+"==Sa=="+smallSubChileAnswer[i]);
                }

                    /*
                    * 选项个数：sumOption
                    * 选项题号：optionIndex[i]
                    * 选项内容：optionContent[i]
                    * */
                sumOption = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().size();
                optionIndex = new String[sumOption];
                optionContent = new String[sumOption];
                for (int i = 0; i < sumOption; i++) {
                    optionIndex[i] = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionIndex();
                    optionContent[i] = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildOptionList().get(i).getOptionContent();
                }


                    /*
                    * 文本内容：词汇填空
                    * */
                subChildAsk = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                String arraySubChildAsk[] = new String[TotalItemCount + 1];
                String subChildAskAll = "";
                for (int i = 0; i < TotalItemCount + 1; i++) {
                    arraySubChildAsk[i] = Control.returnArray(subChildAsk, "____", 4)[i];
                    if (i < TotalItemCount) {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                    } else {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                    }
                }
                subChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

                    /*
                    * 答案解析：analysis
                    **/
                analysis = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }


    }

    /*
    * 显示界面信息
    * */
    private void showQuesInfor(){
        holder.WvTheme.loadDataWithBaseURL("about:blank", subChildAsk, "text/html", "utf-8", null);
        holder.WvTheme.getSettings().setDefaultFontSize(18);
        holder.WvTheme.setFocusable(false);
    }


}